﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software components that provide 
// support for localization. This library enables you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace LocalizationLibrary.Tests.Core
{
    //.NET References
    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;

    //LocalizationLibrary References
    using LocalizationLibrary.Messaging;

    // Microsoft References
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class DynamicResourceTest
    {
        [ExpectedException(typeof(ArgumentException))]
        [TestMethod]
        public void New_Instance_Thrown_ArgumentException_If_ResourceID_Is_Null()
        {
            DynamicResource instance = new DynamicResource(null);
        }

        [ExpectedException(typeof(ArgumentException))]
        [TestMethod]
        public void New_Instance_Thrown_ArgumentException_If_ResourceID_Is_Equals_To_White_Space()
        {
            DynamicResource instance = new DynamicResource(String.Empty);
        }

        [TestMethod]
        public void Publishes_A_Message_If_The_Language_Is_Overriden()
        {
            DynamicResource instance = new DynamicResource(Guid.NewGuid().ToString());
            bool executed = false;
            MessageEventHandler messageHandler = (_sender, _e) => 
                {
                    if (((ResourceMessage)(_e.Message)).Resource == instance)
                    {
                        executed = true;
                    }
                };
            MessagingManager.OnMessage += messageHandler;
            instance.Add((Language)"it", "Ciao", true);
            Assert.AreEqual(executed, true);
            MessagingManager.OnMessage -= messageHandler;
        }

        [TestMethod]
        public void Null_Language_Returns_Null() 
        {
            DynamicResource instance = new DynamicResource(Guid.NewGuid().ToString());
            Object value = instance[null];
            Assert.AreEqual(value, null);
        }

        [TestMethod]
        public void GetHashCode_Should_Be_Always_The_Same()
        {
            String resourceID = Guid.NewGuid().ToString();
            DynamicResource instance = new DynamicResource(resourceID);
            Assert.AreEqual(instance.GetHashCode(), resourceID.GetHashCode());
        }

        [TestMethod]
        public void GetHashCode_Should_Be_Not_Equal()
        {
            DynamicResource instance = new DynamicResource(Guid.NewGuid().ToString());
            DynamicResource instance2 = new DynamicResource(Guid.NewGuid().ToString());
            Assert.AreNotEqual(instance.GetHashCode(), instance2.GetHashCode());
        }

        [TestMethod]
        public void Verify_If_Two_Instance_Are_Equals()
        {
            String resourceID = Guid.NewGuid().ToString();
            DynamicResource instance = new DynamicResource(resourceID);

            Assert.AreEqual(instance.Equals(null), false);
            Assert.AreEqual(instance.Equals(instance), true);

            DynamicResource instance2 = new DynamicResource(resourceID);

            Assert.AreEqual(instance.Equals(instance2), true);

            instance2 = new DynamicResource(Guid.NewGuid().ToString());

            Assert.AreEqual(instance.Equals(instance2), false);
        }
    }
}